// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   SerialModemDriver.java

package cn.sendsms;

import java.io.*;
import java.util.StringTokenizer;
import java.util.TooManyListenersException;
import javax.comm.*;
import org.apache.log4j.Logger;

// Referenced classes of package cn.sendsms:
//			ModemDriver, GatewayException, ModemGateway, AGateway

class SerialModemDriver extends ModemDriver
	implements SerialPortEventListener
{
	private class ThreadReader extends Thread
	{

		private AGateway gtw;

		public void run()
		{
			gtw.getLogger().debug("ThreadReader started...");
			do
				try
				{
					do
					{
						do
							sleep(300L);
						while (!portHasData());
						synchronized (SYNC_Reader)
						{
							dataReceived = true;
							SYNC_Reader.notifyAll();
						}
					} while (true);
				}
				catch (InterruptedException e)
				{
					break;
				}
				catch (Exception e)
				{
					gtw.getLogger().error("ThreadReader: " + e.getMessage());
				}
			while (true);
			gtw.getLogger().debug("ThreadReader stopped...");
		}

		public ThreadReader(AGateway gtw)
		{
			super();
			this.gtw = gtw;
			start();
		}
	}


	private static final boolean THREAD_READ = false;
	private static final int THREAD_READ_WAIT = 300;
	private String comPort;
	private int baudRate;
	private CommPortIdentifier portId;
	private SerialPort serialPort;
	private InputStream in;
	private OutputStream out;
	private ThreadReader threadReader;

	SerialModemDriver(ModemGateway gateway, String deviceParms)
	{
		super(gateway, deviceParms);
		StringTokenizer tokens = new StringTokenizer(deviceParms, ":");
		comPort = tokens.nextToken();
		baudRate = Integer.parseInt(tokens.nextToken());
		serialPort = null;
	}

	void connectPort()
		throws GatewayException, IOException, InterruptedException
	{
		try
		{
			gateway.getLogger().info("Opening: " + comPort + " @" + baudRate);
			portId = CommPortIdentifier.getPortIdentifier(comPort);
			serialPort = (SerialPort)portId.open("SMSLib", 1971);
			in = serialPort.getInputStream();
			out = serialPort.getOutputStream();
			serialPort.notifyOnDataAvailable(true);
			serialPort.notifyOnOutputEmpty(true);
			serialPort.notifyOnBreakInterrupt(true);
			serialPort.notifyOnFramingError(true);
			serialPort.notifyOnOverrunError(true);
			serialPort.notifyOnParityError(true);
			serialPort.setFlowControlMode(1);
			serialPort.addEventListener(this);
			serialPort.setSerialPortParams(baudRate, 8, 1, 0);
			serialPort.setInputBufferSize(16384);
			serialPort.setOutputBufferSize(16384);
			serialPort.enableReceiveTimeout(30000);
			setConnected(true);
		}
		catch (NoSuchPortException e)
		{
			throw new GatewayException("The selected comm port does not exist.");
		}
		catch (PortInUseException e)
		{
			throw new GatewayException("Comm port selected is currently in use.");
		}
		catch (TooManyListenersException e)
		{
			throw new GatewayException("Too many listeners on selected comm port.");
		}
		catch (UnsupportedCommOperationException e)
		{
			throw new GatewayException("Comm parameters are not supported on this comm port");
		}
	}

	void disconnectPort()
		throws IOException, InterruptedException
	{
		gateway.getLogger().info("Closing: " + comPort + " @" + baudRate);
		if (serialPort != null)
			serialPort.close();
		setConnected(false);
	}

	void clear()
		throws IOException
	{
		for (; portHasData(); read());
	}

	boolean portHasData()
		throws IOException
	{
		return in.available() > 0;
	}

	public void serialEvent(SerialPortEvent event)
	{
		switch (event.getEventType())
		{
		case 2: // '\002'
		case 3: // '\003'
		case 4: // '\004'
		case 5: // '\005'
		case 6: // '\006'
		case 10: // '\n'
		default:
			break;

		case 7: // '\007'
			gateway.getLogger().error("Overrun Error!");
			break;

		case 9: // '\t'
			gateway.getLogger().error("Framing Error!");
			break;

		case 8: // '\b'
			gateway.getLogger().error("Parity Error!");
			break;

		case 1: // '\001'
			synchronized (SYNC_Reader)
			{
				dataReceived = true;
				SYNC_Reader.notifyAll();
			}
			break;
		}
	}

	void write(char c)
		throws IOException
	{
		out.write(c);
		out.flush();
	}

	int read()
		throws IOException
	{
		return in.read();
	}
}
